var vueApp = new Vue({
  el: '#app',
  data: function () {
    return {
    	  cascaderdeptOpt:{  			
     		 showallevels:true,  //false 仅显示一级  true 显示多级  	
     		 changeonselect:true,//若需要允许用户选择任意一级选项，则可将change-on-select赋值为true ; false 只能选择最后一级
     		 clearable:true,//可清除
     		 filterable:true,//将filterable赋值为true即可打开搜索功能
     		 defaultProps: {
     			children : 'children',
     			label : 'label',
     			value : 'id'
     		 },
     	  },    		
     	  cascaderdept:{  		    
     		 depts:[],   		  
     		 deptvalue:[], //默认值通过数组的方式指定
     	  },  	
    	  gsbm:'',
    	  createDate:[],
    	  msgType:'',
    	  msgSts:'',
    	  title:'',
    	  mtpOptions:[],
    	  mstOptions:[],
    	  msgData:[],
    	  currentPageNbr: config.currentPageNbr,
          pageSizes: config.pageSizes,
          pageSize: config.defaultpageSize,
          totalRecords: 0,
          addUrl:'msgadd.html',
          delMsgs:[],
          rpyUrl:'comList.html',
          exec:'comlist',
          curRowOption:{},
          comDel:false,
          expands:[],
          curId:''
      };
  },
  mounted: function () {
    this.initData();

  },
  methods: {
    initData:function(){
       var that = this;
       
       this.createDate.push(tools.getQueryString("beginDate")==""?util.addMonth(new Date(),-1):new Date(tools.getQueryString("beginDate").replace(/-/g,"/")));
       this.createDate.push(tools.getQueryString("endDate")==""?new Date():new Date(tools.getQueryString("endDate").replace(/-/g,"/")));
       this.mtpOptions.push({"code":"-1","code_desc":"请选择","display_order":"0"});
       this.mtpOptions.push.apply(this.mtpOptions,msg_type);
       this.msgType = tools.getQueryString("msgType")==""?"-1":tools.getQueryString("msgType");
       this.mstOptions.push({"code":"-1","code_desc":"请选择","display_order":"0"});
       this.mstOptions.push.apply(this.mstOptions,msg_status);
       this.msgSts = tools.getQueryString("msgSts")==""?"msg_status_0":tools.getQueryString("msgSts"); 
       this.cascaderdept=tools.getDeptOpts(parseInt(tools.getQueryString("gsbm")==""?userExInfo.deptCode:tools.getQueryString("gsbm")),"0","0"); 
  	   this.gsbm= tools.getQueryString("gsbm")==""?userExInfo.deptCode:tools.getQueryString("gsbm");
  	   this.title=tools.getQueryString("title");
//       userEx.getUserInfo(function(data){
//            that.user_ssbm = data.userinfo.deptCode; 
//         
//            tools.checkPriv(data.userinfo.privNames,"cbgs");
//           
//       });
//       tools.checkPriv(userExInfo.privNames,"cbgs"); 

       this.onSearch();
       
    },
    
	deptChg:function(value){
  		var arry = tools.deptchange(value,this.cascaderdept.depts);
  		this.gsbm= arry[arry.length-1];
  	},
  	
    titleLink:function(row){
    	var beginDate = this.createDate!=null?util.formatTime(this.createDate[0],'yyyy-MM-dd'):"";
        var endDate = this.createDate!=null?util.formatTime(this.createDate[1],'yyyy-MM-dd'):"";
        window.location.href = this.addUrl+"?msgId=" + row.msg_id+"&msgType="+this.msgType+"&msgSts="
        +this.msgSts+"&gsbm="+this.gsbm+"&title="+this.title+"&beginDate="+beginDate+"&endDate="+endDate+"&oper=read&org=0"; 
//    	window.location.href=this.addUrl+"?msgId="+id+"&oper=read";
    },
    newMsg:function(){
    	var that = this;
//    	this.$refs.bddg.open(this.bdUrl+"?cjid="+row.cjid+"&cjtitle="+row.gjbt+"&v="+Math.random(), {   
//            fullscreen: true, 
//            title: "采编归属绑定", 
//            callback: function(arg) {
//              var item = JSON.parse(arg);
//              if(item.msg!=undefined){
//                that.onSearch();
//              }
//            }
//    	}); 
    	var beginDate = this.createDate!=null?util.formatTime(this.createDate[0],'yyyy-MM-dd'):"";
        var endDate = this.createDate!=null?util.formatTime(this.createDate[1],'yyyy-MM-dd'):"";
    	window.location.href =  this.addUrl+"?msgType="+this.msgType+"&msgSts="
        +this.msgSts+"&gsbm="+this.gsbm+"&title="+this.title+"&beginDate="+beginDate+"&endDate="+endDate+"&oper=add&org=0"; 

    },
    
    choseMsg:function(value){
        this.delMsgs = value;
    },
    
    shorten:function(txt){
    	var result=txt;
    	if(result.length>30){
    		return result.substr(0,30)+"...";
    	}
    	return result;
    },
    delMsg:function(){
    	var that =this;
    	if(this.delMsgs.length==0){
    		this.showError("请至少选择一条消息");        
    		return false;
    	}
    	var delTitle=[];
    	var ids = [];
    	delTitle = this.delMsgs.map(function(item){
    		return that.shorten(item.msg_title);
    	});
    	ids =  this.delMsgs.map(function(item){
    		return "'"+item.msg_id+"'";
    	});
    	
    	that.$confirm('是否确认删除消息 '+delTitle.join(",")+' ?', '提示', {
             confirmButtonText: '确定',
             cancelButtonText: '取消',
             type: 'warning'
        }).then(() => {
        	
        	var jso = {
        			"idstr": ids.join(","),
        			"operType": "delbatch"
        			
        			
        		};
       
        	
        	
        	var reqData={
        		"jsonStr":JSON.stringify(jso),    	
        		
    	    			
    	    };
        	tools.ajaxRequestAsyncEx("pub/msg/messageDel", reqData, function(data){
    	   		if (data.respCode != 0) {
    	   			that.showError(data.respDesc);    
    	   			return false;
    	   		}else{
    	   			that.$message({
    	                showClose: true,
    	                type: 'success',
    	                message: '删除成功!'
    	            });  
    	   			that.onSearch();
    	   		}    	   			
    	    });
        	
        }).catch(() => {
           		return false;
        });
    },
//    chkMsg:function(row){
//    	this.exec ='chk';
//    	this.toggleRow(row);
//    },
//    sortMsg:function(row){
//    	this.exec = 'sort';
//    	this.toggleRow(row);
//    },\
    getRowKeys:function(row) {
        return row.msg_id;
    },
    vhtml:function(row){    	
		var url="news.html?msg_id="+row.msg_id;
		window.open (url);  
    },
    rpyMsg:function(row){
//    	this.toggleRow(row);
    	this.expands = []; 
    	if(this.curRowOption.msgId!=row.msg_id){
    		this.expands.push(row.msg_id);  
    	}else{
    		this.curRowOption.msgId="";
    	}    	   
    	if(this.expands.length>0){
	    	this.exec = 'comlist';
	    	this.curRowOption.msgId = row.msg_id;
	    	this.$nextTick(() => {  
	            this.$refs.clist.onSearch();            
	        });
    	}    	
    },
    toggleRow:function(row){
    	let $table = this.$refs.msgTable;
        this.msgData.map(item => {
          if (row.id != item.id) {
            $table.toggleRowExpansion(item, false);
          }
        });
        $table.toggleRowExpansion(row);
    },    
    editMsg:function(row){
        var beginDate = this.createDate!=null?util.formatTime(this.createDate[0],'yyyy-MM-dd'):"";
        var endDate = this.createDate!=null?util.formatTime(this.createDate[1],'yyyy-MM-dd'):"";
        window.location.href = this.addUrl+"?msgId=" + row.msg_id+"&msgType="+this.msgType+"&msgSts="
        +this.msgSts+"&gsbm="+this.gsbm+"&title="+this.title+"&beginDate="+beginDate+"&endDate="+endDate+"&oper=edit&org=0"; 
    	
    },    
    pubMsg:function(row){
	    var that = this;
	    this.$confirm('是否确认发布当前消息 '+that.shorten(row.msg_title)+' ?', '提示', {
	      confirmButtonText: '确定',
	      cancelButtonText: '取消',
	      type: 'warning'
	    }).then(() => {
	    	
	    	var jso = {
        			"msgId": row.msg_id,
        			"operType": "send"        			
        		};       	
        	
        	var reqData={
        		"jsonStr":JSON.stringify(jso),    	    	    			
    	    };
	    
	    	tools.ajaxRequestAsyncEx("pub/msg/messageSend", reqData, function(data){
		   		if(data.respCode !=0){
		   			that.showError(data.respDesc);   
		   			return false;
			    }else{
			    	that.$message({
    	                showClose: true,
    	                type: 'success',
    	                message: '发布成功!'
    	            }); 
			    	that.onSearch();
			    }
		   	});
	    	
	    }).catch(() => {
	    	return false;
	    });    	
    },

    onSearch:function(){
      var that = this;
      var beginDate = this.createDate!=null?util.formatTime(this.createDate[0],'yyyy-MM-dd'):"";
      var endDate = this.createDate!=null?util.formatTime(this.createDate[1],'yyyy-MM-dd'):"";
      var jso = {
    			"startDate": beginDate,
    			"endDate": endDate,
    			"msgType": this.msgType,    		
    			"msgStatus":this.msgSts,
    			"gsbm":this.gsbm,
    			"msg_title": this.title
    		};
      var postdata = {
    	    "jsonStr":JSON.stringify(jso),    		 
            "currentPageNbr" : this.currentPageNbr,
            "pageSize" : this.pageSize
      };
      
      tools.ajaxRequestAsyncEx("pub/msg/messageList",postdata, function(data){
          if (data.respCode == "0") {
              that.msgData = data.list;
              that.totalRecords = data.page.totalRecords;       
          } else {
              that.msgData = [];
              that.totalRecords =0;
              that.currentPageNbr=1;
              that.showError(data.respDesc);      
          }
      });


    },
    
    search:function(){
    	this.currentPageNbr = 1;
        this.onSearch();
    },
    handleSizeChange:function(pageSize){
        this.pageSize = pageSize;
        this.currentPageNbr = 1;
        this.onSearch();
    },

    handleCurrentChange:function(currentPageNbr){
      this.currentPageNbr = currentPageNbr;
      this.onSearch();
    },

    showError: function(info){
        var i = (info==undefined?"加载数据错误":info);
        this.$alert(i, '系统错误', {
             confirmButtonText: '确定'
        }); 
    },

  },
  watch:{
//      currentPageNbr : function() {
//          // alert(this.currentPageNbr);
//          this.onSearch();
//      },
//        
//      pageSize : function() {
//          // alert(this.currentPageNbr);
//    	  if(this.currentPageNbr=="1"){
//    		  this.onSearch();
//    	  }else{
//    		  this.currentPageNbr=="1";
//    	  }         
//      },
  }
})